<?php
namespace app\admin\behavior;

use think\Controller;
use think\Model;
use think\Loader;

use app\common\model\AdminRule;
use app\common\model\AdminUserGroup;
use app\common\model\AdminUsers;
use app\common\model\AdminNav;


/**
 * @author Yel 2019/7/24
 * 权限控制行为
 */
class Rules{

	use \traits\controller\Jump;//类里面引入jump;类

	/**
	 * 后台权限检测
	 * @author Yel 2019/7/24
	 */
	public function checkRule(&$params){
		//当前访问的模块名
        $module = request()->module();
        //当前访问的控制器
        $controller = request()->controller();
        //当前访问的方法
        $action = request()->action();

		$rule_name = strtolower($module.'/'.$controller.'/'.$action);
		//欢迎页没有权限
		if($rule_name == 'admin/index/index'){
			return;
		}
		//获取权限列表
		$Rule  = $this->getUserRuleId();
		//检测是否有权限
		foreach ($Rule as $key => $value) {
			if(strtolower($value['mac']) == $rule_name){
				return;
			}
		}
		//判断是不是post 提交
		if(request()->isPost()){
			$this->result('',-1,'您没有权限','json');
		}
		$this->redirect('errormsg/put_error/notRule');
		
	}

	//-----------------------------------------


	/**
	 * 获取用户的权限
	 * @author Yel  2019/7/5
	 * @copyright  yel
	 * @return array  返回自己的权限列表
	 */
	private function getUserRuleId(){
		//获取用户数据
		$user = session('user');
		//获取用户角色
		$User = new AdminUsers();
		$userInfo = $User->findData($user['id'],'admin_user_group_id');
		if(empty($userInfo)){
			$this->redirect('admin/login/index');
		}
		$group_id = $userInfo['admin_user_group_id'];

		//获取自己角色的权限
		$Group = new AdminUserGroup();
		$groupInfo = $Group->findData($group_id,'rule,status');
		if(empty($groupInfo)){
			$this->redirect('admin/login/index',['login_error'=>true]);
		}
		if($groupInfo['status'] == 0){
			$this->redirect('admin/login/index',['login_error'=>true]);
		}
		//根据自己角色权限ID 获取权限值
		$Rule = new AdminRule();
		$ruleInfo = $Rule->selectData(['id'=>['in',$groupInfo['rule']]]);
		return $ruleInfo;
	}



}